package com.example.oj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.oj.entity.CompetitionResult;
import com.example.oj.entity.Question;
import com.example.oj.entity.User;
import com.example.oj.entity.dto.competition.ResultQueryRequest;
import com.example.oj.entity.vo.CompetitionResultVO;
import com.example.oj.entity.vo.UserVO;
import com.example.oj.service.CompetitionResultService;
import com.example.oj.mapper.CompetitionResultMapper;
import com.example.oj.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 28651
* @description 针对表【competitionResult】的数据库操作Service实现
* @createDate 2024-05-04 15:04:02
*/
@Service
public class CompetitionResultServiceImpl extends ServiceImpl<CompetitionResultMapper, CompetitionResult>
    implements CompetitionResultService {

  @Resource
  private CompetitionResultMapper mapper;

  @Resource
  private UserService userService;

  @Override
  public void createResult(Long id) {
    mapper.createResult(id);
  }

  @Override
  public Page<CompetitionResultVO> selectResultById(ResultQueryRequest request) {
    Long id = request.getId();
    long current = request.getCurrent();
    long pageSize = request.getPageSize();
    QueryWrapper<CompetitionResult> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("competitionId",id);
    queryWrapper.orderByDesc("score");
    queryWrapper.orderByAsc("length");
    List<CompetitionResult> competitionResults = mapper.selectList(queryWrapper);
    List<CompetitionResultVO> resultVOList = competitionResults.stream().map(this::getCompetitionResultVO).collect(Collectors.toList());
    Page<CompetitionResultVO> questionPage = new Page<>(current, pageSize);
    questionPage.setRecords(resultVOList);
    return questionPage;
  }

  @Override
  public CompetitionResultVO getCompetitionResultVO(CompetitionResult entity) {
    CompetitionResultVO competitionResultVO = new CompetitionResultVO();
    competitionResultVO.setId(entity.getId());
    User user = userService.getById(entity.getUserId());
    UserVO userVO = userService.getUserVO(user);
    competitionResultVO.setUser(userVO);
    competitionResultVO.setScore(entity.getScore());
    competitionResultVO.setLength(entity.getLength());
    competitionResultVO.setRanking(entity.getRanking());
    return competitionResultVO;
  }
}




